package com.foreveross.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.common.Constants;
import com.foreveross.dao.BaseDAO;
import com.foreveross.dao.ExpressDAO;

@Service("baseService")
@Transactional
public class BaseService {

	@Resource
	public BaseDAO baseDAO;
	
	public <T extends Object> void save(T entity) throws Exception{
		baseDAO.save(entity);
	}
	
	public <T extends Object> T updateObject(T t) throws Exception{
		return baseDAO.updateObject(t);
	}
	
	public <T> T updateObject(T t,String id) throws Exception{
		return baseDAO.updateObject(t,id);
	}
	
	public <T> void saveOrUpdate(T entity, String type) throws Exception {
		if (type.equals(Constants.UPDATE_ADD)) {
			baseDAO.save(entity);
		} else if (type.equals(Constants.UPDATE_EDIT)) {
			baseDAO.updateObject(entity);
		}
	}
	
	public <T> void saveOrUpdate(T entity, String type,String id) throws Exception {
		if (type.equals(Constants.UPDATE_ADD)) {
			baseDAO.save(entity);
		} else if (type.equals(Constants.UPDATE_EDIT)) {
			baseDAO.updateObject(entity,id);
		}
	}

	public void deleteObjec(String tableObj, String id) throws Exception {
		baseDAO.deleteObjec(tableObj, id);
	}

	public Object findObject(Class clazz, String id) throws Exception {
		return baseDAO.findObject(clazz, id);
	}

	public void deleteObjecList(String tableObj, String ids) throws Exception {
		String idArray[] = ids.split(",");
		for (int i = 0; i < idArray.length; i++) {
			deleteObjec(tableObj, idArray[i]);
		}
	}
}
